package com.xcity.game.arena.online.template;

import java.util.concurrent.TimeUnit;

import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;
import naga.x.util.NumberUtils;
import naga.x.util.StringUtils;

/**
 * 在线竞技开启时间
 * @author yang.li
 *
 */
@Excel(value = "arena.xlsx", sheet = 2)
public class ArenaOpenTimeTemplate extends Template {

	/** 从周几 */
	protected int dayFrom;

	/** 到周几 */
	protected int dayTo;

	/** 时间（0:开始时间,1:结束时间,2:开始时间,3:结束时间,...） */
	protected String[] times;
	protected long[] timesMillis;

	public int getDayFrom() {
		return dayFrom;
	}

	public void setDayFrom(int dayFrom) {
		this.dayFrom = dayFrom;
	}

	public int getDayTo() {
		return dayTo;
	}

	public void setDayTo(int dayTo) {
		this.dayTo = dayTo;
	}

	public String[] getTimes() {
		return times;
	}

	public void setTimes(String[] times) {
		this.times = times;
	}

	public long[] getTimesMillis() {
		return timesMillis;
	}

	@Override
	public void validate() throws TemplateException {
		timesMillis = new long[times.length];
		for (int i = 0; i < times.length; i += 2) {
			long beginTime = toMillis(times[i]);
			long endTime = toMillis(times[i + 1]);
			if (endTime < beginTime) { // 结束时间可能是第二天
				endTime += TimeUnit.DAYS.toMillis(1);
			}
			timesMillis[i] = beginTime;
			timesMillis[i + 1] = endTime;
		}
	}
	
	private static long toMillis(String time) {
		// hour:minute:second
		String[] strs = StringUtils.split(time, ":");
		long millis = 0;
		if (strs.length > 0) {
			millis += TimeUnit.HOURS.toMillis(NumberUtils.parseInt(strs[0]));
		}
		if (strs.length > 1) {
			millis += TimeUnit.MINUTES.toMillis(NumberUtils.parseInt(strs[1]));
		}
		if (strs.length > 2) {
			millis += TimeUnit.SECONDS.toMillis(NumberUtils.parseInt(strs[2]));
		}
		return millis;
	}
}
